#include <jni.h>
#include <string>
#include <vector>
#include "DataHelper.hpp"
extern "C" JNIEXPORT jobject JNICALL
Java_com_netease_rtcdemo_StatsHelper_downsample(
        JNIEnv* env,
        jobject /* this */,
        jobject list,
        jint max_size) {
    jclass arrayListClass = env->FindClass("java/util/ArrayList");
    jmethodID sizeMethod = env->GetMethodID(arrayListClass, "size", "()I");
    jmethodID getMethod = env->GetMethodID(arrayListClass, "get", "(I)Ljava/lang/Object;");
    jclass integerClass = env->FindClass("java/lang/Integer");
    jmethodID intValueMethod = env->GetMethodID(integerClass, "intValue", "()I");

    jint size = env->CallIntMethod(list, sizeMethod);
    std::vector<int> intVector;

    for(jint i = 0;i < size;i++){
        jobject integerObj = env->CallObjectMethod(list, getMethod, i);
        jint intValue = env->CallIntMethod(integerObj, intValueMethod);
        intVector.push_back(intValue);
    }

    std::vector<int> resultVector = NTES_Stats::downsample(intVector, max_size);

    // 创建一个新的 ArrayList
    jobject resultList = env->NewObject(arrayListClass, env->GetMethodID(arrayListClass, "<init>", "()V"));
    // 获取 ArrayList 的 add 方法
    jmethodID addMethod = env->GetMethodID(arrayListClass, "add", "(Ljava/lang/Object;)Z");

    // 将处理后的整数添加到新的 ArrayList 中
    for (int num : resultVector) {
        jobject integerObj = env->NewObject(integerClass, env->GetMethodID(integerClass, "<init>", "(I)V"), num);
        env->CallBooleanMethod(resultList, addMethod, integerObj);
    }
    return resultList;
}

extern "C" JNIEXPORT jstring JNICALL
Java_com_netease_rtcdemo_StatsHelper_buildHTML(
        JNIEnv* env,
        jobject /* this */,
        jobject list,
        jobject list_title,
        jobject list_name,
        jstring htmlTemplate) {
    jclass arrayListClass = env->FindClass("java/util/ArrayList");
    jmethodID sizeMethod = env->GetMethodID(arrayListClass, "size", "()I");
    jmethodID getMethod = env->GetMethodID(arrayListClass, "get", "(I)Ljava/lang/Object;");
    jclass integerClass = env->FindClass("java/lang/Integer");
    jmethodID intValueMethod = env->GetMethodID(integerClass, "intValue", "()I");

    // 获取最外层 ArrayList 的大小
    jint outerSize = env->CallIntMethod(list, sizeMethod);
    std::vector<std::vector<int>> nestedVector;

    for (jint i = 0; i < outerSize; i++) {
        jobject innerListObj = env->CallObjectMethod(list, getMethod, i);
        // 获取内层 ArrayList 的大小
        jint innerSize = env->CallIntMethod(innerListObj, sizeMethod);
        std::vector<int> innerVector;

        for (jint j = 0; j < innerSize; j++) {
            // 获取内层 ArrayList 中的 Integer 对象
            jobject integerObj = env->CallObjectMethod(innerListObj, getMethod, j);
            // 获取 Integer 对象的 int 值
            jint intValue = env->CallIntMethod(integerObj, intValueMethod);
            innerVector.push_back(intValue);
        }

        nestedVector.push_back(innerVector);
    }
    jint size = env->CallIntMethod(list_name, sizeMethod);
    std::vector<std::string> stringVector;

    for(jint i = 0;i < size;i++){
        jobject stringObj = env->CallObjectMethod(list_name, getMethod, i);
        const char* cStr = env->GetStringUTFChars((jstring)stringObj, nullptr);
        if (cStr != nullptr) {
            stringVector.emplace_back(cStr);
            env->ReleaseStringUTFChars((jstring)stringObj, cStr);
        }
    }

    size = env->CallIntMethod(list_title, sizeMethod);
    std::vector<std::string> titleVector;

    for(jint i = 0;i < size;i++){
        jobject stringObj = env->CallObjectMethod(list_title, getMethod, i);
        const char* cStr = env->GetStringUTFChars((jstring)stringObj, nullptr);
        if (cStr != nullptr) {
            titleVector.emplace_back(cStr);
            env->ReleaseStringUTFChars((jstring)stringObj, cStr);
        }
    }

    const char* cStr = env->GetStringUTFChars(htmlTemplate, nullptr);
    std::string html_template;
    if (cStr != nullptr) {
        html_template = cStr;
        env->ReleaseStringUTFChars(htmlTemplate, cStr);

    }

    std::vector<std::pair<std::string, std::vector<int>>> charts;
    for(int i = 0;i < nestedVector.size();i++){
        charts.push_back({titleVector[i], nestedVector[i]});
    }

    std::string html = NTES_Stats::BuildHTML(stringVector, charts, html_template);

    return env->NewStringUTF(html.c_str());;
}